package com.richfit.barcodesystem_cczt.fragments;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.richfit.barcodesystem_cczt.R;
import com.richfit.barcodesystem_cczt.activitis.Main;
import com.richfit.barcodesystem_cczt.activitis.adapters.BottomDialogMenuAdapter;
import com.richfit.barcodesystem_cczt.activitis.model.base.Result;
import com.richfit.barcodesystem_cczt.activitis.model.login.BottomMenuEntity;
import com.richfit.barcodesystem_cczt.databinding.FragmentPositionAdjustmentBinding;
import com.richfit.barcodesystem_cczt.fragments.base.BaseFragment;
import com.richfit.barcodesystem_cczt.fragments.model.ZBoxEntity;
import com.richfit.barcodesystem_cczt.network.OkHttpUtils;
import com.richfit.barcodesystem_cczt.utils.Urls;
import com.richfit.barcodesystem_cczt.utils.dialog.DialogHelper;
import com.richfit.barcodesystem_cczt.utils.toast.ToastUtil;
import com.richfit.barcodesystem_cczt.widget.RichAutoEditText;

import java.util.ArrayList;
import java.util.List;


public class PositionAdjustment extends BaseFragment {
    private FragmentPositionAdjustmentBinding binding;

    private String bizType;

    public PositionAdjustment(String bizType) {
        this.bizType = bizType;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentPositionAdjustmentBinding.inflate(inflater, container, false);
        initView();
        initData();
//        binding.icdCollection.etBoxNum.setText("zbox-zyl-010001");
//        getRefData("zbox-zyl-010001");
        return binding.getRoot();
    }

    private void initView() {
    }

    private void initData() {
        //箱号输入后并点击
        binding.icdCollection.etBoxNum.setOnRichAutoEditTouchListener((view, text) -> {
            getRefData(text);
        });
        binding.icdCollection.floatingButton.setOnClickListener(v -> {
            //目标仓位
            String M_location = binding.icdCollection.EdtLocation.getText().toString().trim();
            //源仓位
            String Y_location = binding.icdCollection.etLocation.getText().toString().trim();
            //判断是否输入了仓位，没有输入仓位，给出提示信息
            if (TextUtils.isEmpty(M_location)) {
                ToastUtil.showTextShort("请输入目标仓位！", binding.getRoot());
                return;
            } else if (M_location.equals(Y_location)) {
                //源仓位和目标仓位相同，给出提示
                ToastUtil.showTextShort("源仓位和目标仓位相同", binding.getRoot());
                return;
            }
            shwoChooseDialog();
        });
        //控件拖动
        setViewMove(binding.icdCollection.floatingButton);
    }

    @Override
    public void handleBarCodeScanResult(String type, String[] list) {
        if (list != null) {
            //获取当前焦点所在的控件
            View vFocus = this.getActivity().getWindow().getDecorView().findFocus();

            if (vFocus instanceof RichAutoEditText) {
                RichAutoEditText richAutoEditText = (RichAutoEditText) vFocus;
                if (richAutoEditText.getId() == binding.icdCollection.etBoxNum.getId()) {
                    //抬头 装箱单
                    binding.icdCollection.etBoxNum.setText(list[0]);
                    getRefData(list[0]);
                    binding.icdCollection.etBoxNum.setText("");
                } else if (richAutoEditText.getId() == binding.icdCollection.etLocation.getId()) {
                    //抬头  仓位
                    binding.icdCollection.etLocation.setText(list[0]);
                }

            }
            ToastUtil.showTextShort(list[0]);
        }
    }

    /**
     * 查询箱单信息
     *
     * @param zboxBarcode
     */
    private void getRefData(String zboxBarcode) {
        DialogHelper dialogHelper = new DialogHelper(this.getActivity());
        dialogHelper.showLoadingDialog("正在查询装箱单信息");
        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        okHttpUtils.doGet(Urls.URL_ZBOX_LINE + "?zboxBarcode=" + zboxBarcode, bo.cookie, new OkHttpUtils.NetCallBack() {

            @Override
            public void onSuccess(Result result) {
                dialogHelper.dismissDialog();
                String data = gson.toJson(result.getData());
                ZBoxEntity.Line line = gson.fromJson(data, ZBoxEntity.Line.class);
                setItemContent(line);
            }

            @Override
            public void onFailure(Exception e) {
                dialogHelper.dismissDialog();
            }
        });
    }

    /**
     * 填充数据
     *
     * @param line
     */
    private void setItemContent(ZBoxEntity.Line line) {
        //箱子ID
        binding.icdCollection.etBoxNum.setTag(line.getId());
        //箱号清空
        binding.icdCollection.etBoxNum.setText("");
        //箱号显示
        binding.icdCollection.TxtBoxNum.setText(line.getZboxBarcode());
        //装箱单号
        binding.icdCollection.etFboxNum.setText(line.getZboxBarcode().substring(0, line.getZboxBarcode().length() - 4));

        //包装方式
        binding.icdCollection.etPackageManner.setText(line.getPackingSpec());
        //长
        binding.icdCollection.etLength.setText(line.getsLength());
        //宽
        binding.icdCollection.etWidth.setText(line.getsWidth());
        //高
        binding.icdCollection.etHeight.setText(line.getsHeight());
        //体积
        binding.icdCollection.etVolume.setText(line.getsVolume());
        //净重
        binding.icdCollection.etNetWeight.setText(line.getsNetWeight());
        //毛重
        binding.icdCollection.etGrossWeight.setText(line.getsGrossWeight());
        //上架仓位
        binding.icdCollection.etLocation.setText(line.getLocation());

    }

    /**
     * 箱号采集-调整分箱单到其他箱子
     * 显示操作保存按钮，弹窗
     */
    private void shwoChooseDialog() {
        View rootView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_bottom_menu, null);
        GridView menu = rootView.findViewById(R.id.gd_menus);
        ArrayList<BottomMenuEntity> items = new ArrayList<>();
        BottomMenuEntity item = new BottomMenuEntity();
        item.setMenuName("保存");
        item.setMenuImageRes(R.mipmap.icon_data_submit);
        items.add(item);
        BottomDialogMenuAdapter adapter = new BottomDialogMenuAdapter(this.getActivity(), items);
        menu.setAdapter(adapter);
        Dialog dialog = setDialog(rootView);
        menu.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:
                    List<ZBoxEntity.CommitLine> commit_lines = new ArrayList<>();
                    ZBoxEntity.CommitLine commitLine = new ZBoxEntity.CommitLine();
                    commitLine.setZboxLineId(binding.icdCollection.etBoxNum.getTag().toString());
                    commit_lines.add(commitLine);
                    ZBoxEntity.Commit commit = new ZBoxEntity.Commit();

                    commit.setRefLines(commit_lines);
                    commit.setTargetLocation(binding.icdCollection.EdtLocation.getText().toString().trim());
                    Commit_Location(commit);
                    break;
                default:
                    break;
            }

            dialog.dismiss();
        });
    }

    /**
     * 调整分箱单的箱子到其他分箱单
     */
    private void Commit_Location(ZBoxEntity.Commit commit) {
        DialogHelper dialogHelper = new DialogHelper(this.getActivity());
        dialogHelper.showLoadingDialog("正在提交数据！");
        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        commit.setCommitType("SUBMIT");
        commit.setBizType("MOVE_LOCATION");

        okHttpUtils.doPostJson(Urls.URL_ZBOX_LOCATION, bo.cookie, gson.toJson(commit), new OkHttpUtils.NetCallBack() {
            @Override
            public void onSuccess(Result result) {
                dialogHelper.dismissDialog();
//                String data = gson.toJson(result.getData());
//                {"info":"操作成功","status":"ok"}
                //保存成功后，需要把箱子，从原来的分箱单中删掉，目标分箱单增加
                ToastUtil.showTextShort("仓位调整成功！");
                Main main = (Main) getActivity();
                main.setFragments(bizType);
            }

            @Override
            public void onFailure(Exception e) {
                dialogHelper.dismissDialog();
            }
        });
    }
}